package com.smartboot.plus.utils;

import java.util.List;

public class SqlUtil {

	// 生成分页查询 SQL，自动处理查询条件拼接
	public static String getQuery(String tableName, List<String> queryConditions, int pageSize, int currentPage) {
		StringBuilder sql = new StringBuilder("SELECT * FROM ").append(tableName).append(" WHERE 1 = 1 ");

		// 动态拼接查询条件
		if (queryConditions != null && !queryConditions.isEmpty()) {
			for (String condition : queryConditions) {
				sql.append(" AND ").append(condition);
			}
		}

		// 处理分页
		if (pageSize > 0) {
			long offset = Math.max((currentPage - 1L) * pageSize, 0L);
			sql.append(" LIMIT ").append(pageSize).append(" OFFSET ").append(offset);
		}
		return sql.toString();
	}
	public static String getQuery(String tableName) {
		StringBuilder sql = new StringBuilder("SELECT * FROM ").append(tableName).append(" WHERE 1 = 1 ");
		// 处理分页
		return sql.toString();
	}

	/**
	 * 生成获取总记录数的 SQL
	 *
	 * @param tableName     表名
	 * @param queryConditions 查询条件
	 * @return 统计记录的 SQL
	 */
	public static String getCountQuery(String tableName, List<String> queryConditions) {
		StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM ").append(tableName).append(" WHERE 1 = 1");

		if (queryConditions != null && !queryConditions.isEmpty()) {
			for (String condition : queryConditions) {
				sql.append(" AND ").append(condition);
			}
		}

		return sql.toString();
	}
}
